<?php

/** User Class
 * @author Igor.Barkowsky@gmail.com
 * @copyright 2009
 * @package schs-cms
 */
class User
{
	var $db;
	var $user_table='users';
	var $data=array();
	var $pfx = 'site';
	var $login;
	var $id;
	var $user_id;
	var $ip;
	function __construct(MyDB $db)
	{
		$this->db = $db;
		$this->user_table = $this->db->pfx.$this->user_table;
		$is_user = $this->check_user();
		$this->ip = $this->get_client_ip();
		if($is_user != false)
		{
			$this->get_user_data($this->user_id);
			$this->login = $this->data['login'];
		}
		else
		{
			$this->data = array('user_id'=>0,'ip'=>$this->ip);
		}
		$this->set_user_data_to();
	}
	function check_access()
	{
		$is_user = $this->check_user();
	}
	function check_user()
	{
		
		$user_data = array();
		if(isset($_SESSION[$this->pfx]))
		{
			$data_from = 's';
			$user_data = $_SESSION[$this->pfx];
			$this->unset_user_data_from();
		}
		elseif(isset($_POST[$this->pfx]))
		{
			$data_from = 'p';
			$user_data = $_POST[$this->pfx];
			unset($_POST[$this->pfx]);
			$this->unset_user_data_from();
		}
		elseif(isset($_COOKIE[$this->pfx]))
		{
			$data_from = 'c';
			$user_data = unserialize($_COOKIE[$this->pfx]);
			$this->unset_user_data_from();
			//print_var($user_data,"cookies");die();
		}
		if(!empty($user_data))
		{
			foreach($user_data as $k=>$v){$user_data[$k]=mysql_real_escape_string($v);}
			if(isset($user_data['login']) and strlen($user_data['login'])>0 and isset($user_data['password']) and strlen($user_data['password'])>0)
			{
				if($data_from=='p')
				{
					$login = $user_data['login'];
					$password = md5($user_data['password']);
				}
				else
				{
					$login = $user_data['login'];
					$password = $user_data['password'];
				}
				$is_registered_user = $this->db->fetch_one("SELECT user_id FROM ".$this->user_table." WHERE login='".$login."' AND password='".$password."'",MYSQL_ASSOC);
				if($is_registered_user!==false and intval($is_registered_user['user_id'])>0)
				{
					$this->user_id = intval($is_registered_user['user_id']);
					return true;
				}
				else
				{
					$this->user_id = 0;
					return false;
				}
			}
			else
			{
				$this->user_id = 0;
				return false;
			}
		}
		else
		{
			$this->user_id = 0;
			return false;
		}
	}
	function get_user_data($id)
	{
		$id=mysql_real_escape_string($id);
		$sql_get_user_data = "
		SELECT 
		u.*,ui.*,ug.* 
		FROM ".$this->user_table." AS u 
		LEFT JOIN ".$this->db->pfx."users_info AS ui ON u.user_id=ui.user_id 
		LEFT JOIN ".$this->db->pfx."users_groups AS ug ON u.group_id=ug.group_id 
		WHERE u.user_id='".$id."'
		";
		$user_data = $this->db->fetch_one($sql_get_user_data,MYSQL_ASSOC);
		if($user_data!==false)
		{
			$user_data['ip'] = $this->get_client_ip();
			$this->data=$user_data;
			return $this->data;
		}
		else return false;
	}
	function set_user_data_to($where='sc')
	{
		if(stripos($where,"s")!==false) $_SESSION[$this->pfx]=$this->data;
		if(stripos($where,"c")!==false) setcookie($this->pfx,serialize($this->data),time()+18144000,"/",".".SITE_DOMAIN,0);
	}
	function unset_user_data_from($where='sc')
	{
		if(stripos($where,"s")!==false){if(isset($_SESSION[$this->pfx])) unset($_SESSION[$this->pfx]);}
		if(stripos($where,"c")!==false){setcookie($this->pfx,"",time(),"/",".".SITE_DOMAIN,0);if(isset($_COOKIE[$this->pfx]))unset($_COOKIE[$this->pfx]);}
	}
	function logout($redirect="/")
	{
		$this->unset_user_data_from();
		header('location: '.$redirect);die();
	}
	function get_client_ip()
	{
	    $ip = false;
		if(getenv("HTTP_CLIENT_IP"))
		{
			$ip = getenv("HTTP_CLIENT_IP");
		}
		elseif(getenv("HTTP_X_FORWARDED_FOR"))
		{
			$ip = getenv("HTTP_X_FORWARDED_FOR");
		}
		elseif(getenv("REMOTE_ADDR"))
		{
			$ip = getenv("REMOTE_ADDR");
		}
		return $ip;
	}
}


?>